/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.service.professor;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import edu.uwm.universitydb.dao.professor.DeleteProfessorCommand;
import edu.uwm.universitydb.dao.professor.InsertProfessorCommand;
import edu.uwm.universitydb.dao.professor.InsertProfessorWorkDepartmentCommand;
import edu.uwm.universitydb.dao.professor.SearchProfessorBySSNCommand;
import edu.uwm.universitydb.dao.professor.SearchProfessorsByNameCommand;
import edu.uwm.universitydb.dao.professor.UpdateProfessorCommand;
import edu.uwm.universitydb.dao.project.management.RemoveProjectManagementAssociationCommand;
import edu.uwm.universitydb.db.connection.DBConnectionFactory;
import edu.uwm.universitydb.framework.exception.DataAccessException;
import edu.uwm.universitydb.model.Department;
import edu.uwm.universitydb.model.Professor;

/**
 * <code>ProfessorServiceImpl</type>
 * implementation for professor serive
 * 
 * @author Shyam Nair
 */
public class ProfessorServiceImpl implements ProfessorService
{

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.service.professor.ProfessorService#addProfessor(edu.uwm.universitydb.model.Professor)
     */
    @Override
    public boolean addProfessor(final Professor professor) throws DataAccessException
    {

        // connection
        Connection dbConnection = null;
        try
        {
            // obtain a connection from factory
            final DBConnectionFactory connectionFactory = DBConnectionFactory.getInstance();
            dbConnection = connectionFactory.getDBConnection();
            dbConnection.setAutoCommit(false);

            // Insert Project
            final InsertProfessorCommand insertCommand = new InsertProfessorCommand(professor);
            int insertCount = insertCommand.executeCommand(dbConnection);

            if (insertCount > 0)
            {
                // create an association if works table
                final String professorSSN = professor.getSsnNum();
                final Department dept = professor.getDepartment();
                final Integer dno = dept.getDeptId();
                final InsertProfessorWorkDepartmentCommand insert =
                    new InsertProfessorWorkDepartmentCommand(professorSSN, dno);
                insertCount = insert.executeCommand(dbConnection);
            }

            // commit
            dbConnection.commit();
            return (insertCount > 0) ? true : false;

        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        catch (final SQLException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        finally
        {
            if (dbConnection != null)
            {
                try
                {
                    dbConnection.close();
                }
                catch (final SQLException e)
                {
                    // DO NOTHING
                    e.printStackTrace();
                }
            }
        }
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.service.professor.ProfessorService#removeProfessor(edu.uwm.universitydb.model.Professor)
     */
    @Override
    public boolean removeProfessor(final String profSSN)
    {
        // connection
        Connection dbConnection = null;
        try
        {
            // obtain a connection from factory
            final DBConnectionFactory connectionFactory = DBConnectionFactory.getInstance();
            dbConnection = connectionFactory.getDBConnection();
            dbConnection.setAutoCommit(false);

            // remove association
            final RemoveProjectManagementAssociationCommand dbUserCommand =
                new RemoveProjectManagementAssociationCommand(profSSN);
            dbUserCommand.executeCommand(dbConnection);

            // delete professor
            final DeleteProfessorCommand userCommand = new DeleteProfessorCommand(profSSN);
            userCommand.executeCommand(dbConnection);

            // commit the transaction
            dbConnection.commit();

            return true;

        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        catch (final SQLException e)
        {
            e.printStackTrace();
            try
            {
                dbConnection.rollback();
            }
            catch (final SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }
            return false;
        }
        finally
        {
            if (dbConnection != null)
            {
                try
                {
                    dbConnection.close();
                }
                catch (final SQLException e)
                {
                    // DO NOTHING
                    e.printStackTrace();
                }
            }
        }
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.service.professor.ProfessorService#searchProfessorByName(java.lang.String)
     */
    @Override
    public List<Professor> searchProfessorByName(final String name)
    {
        try
        {
            final SearchProfessorsByNameCommand searchCommand =
                new SearchProfessorsByNameCommand(name);
            return searchCommand.executeCommand();
        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.service.professor.ProfessorService#getAllProfessors()
     */
    @Override
    public List<Professor> getAllProfessors()
    {
        try
        {
            final SearchProfessorsByNameCommand searchCommand = new SearchProfessorsByNameCommand();
            return searchCommand.executeCommand();
        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.service.professor.ProfessorService#searchProfessorBySSN(java.lang.String)
     */
    @Override
    public Professor searchProfessorBySSN(final String name)
    {
        try
        {
            final SearchProfessorBySSNCommand searchCommand = new SearchProfessorBySSNCommand(name);
            return searchCommand.executeCommand();
        }
        catch (final DataAccessException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean updateProfessor(final Professor professor) throws DataAccessException
    {
        final UpdateProfessorCommand updateCommand = new UpdateProfessorCommand(professor);
        final int updateCount = updateCommand.executeCommand();
        return (updateCount > 0) ? true : false;
    }

}

/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 19, 2014   Shyam Nair
 * File created
 * ----------------------------------------------------------------------------
 */